const { query } = require("express");
let db = require("../db/index");

// 房屋管理列表查询
exports.houseing = (req, res) => {
    var sql =
        `select * from housing order by date asc`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 房屋管理列表条件查询
exports.houseingselect = (req, res) => {
    var sql =
        `select * from housing where Belongingcommunity="${req.query.name}"`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 房屋管理绑定用户(根据房屋id查询房屋信息)
exports.houseingselectowner = (req, res) => {
    
    var sql =
        `select * from housing where date="${req.query.date}"`;
    db.query(sql, [], (err, data) => {
        console.log(sql)
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            data,
            state:200
        });
    });
};
// 添加房屋
exports.houseingadd = (req, res) => {
    var sql = `insert into housing(Belongingcommunity,Buildingname,Numberofunits,RoomNo,buildingarea,usablearea,Housetype,Housingstatus,edit,del,opertes,view,room,office,wash,Sharedarea,StorageRoom,parkinglot,textarea)values('${req.query.Belongingcommunity}','${req.query.Buildingname}',${req.query.Numberofunits},'${req.query.RoomNo}','${req.query.buildingarea}','${req.query.usablearea}','${req.query.Housetype}','${req.query.Housingstatus}','绑定业主','编辑','删除','查看','${req.query.room}','${req.query.office}','${req.query.wash}','${req.query.Sharedarea}','${req.query.StorageRoom}','${req.query.parkinglot}','${req.query.textarea}')`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }
        res.send({
            data,
            state:200
        });
    });
};
// 修改房屋
exports.houseingchange = (req, res) => {
    var sql = `update housing set Belongingcommunity='${req.query.Belongingcommunity}',Buildingname='${req.query.Buildingname}',Numberofunits='${req.query.Numberofunits}',RoomNo='${req.query.RoomNo}',buildingarea='${req.query.buildingarea}',usablearea='${req.query.usablearea}',Housetype='${req.query.Housetype}',Housingstatus='${req.query.Housingstatus}',room='${req.query.room}',office='${req.query.office}',wash='${req.query.wash}',Sharedarea='${req.query.Sharedarea}',StorageRoom='${req.query.StorageRoom}',parkinglot='${req.query.parkinglot}',textarea='${req.query.textarea}'  where date=${req.query.date} `;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }
        res.send({
            data,
            state:200
        });
    });
};
// 房屋绑定业主
exports.houseingbingdingowner = (req, res) => {
    var sql =
        `update housing set owner_id=${req.query.id} where date="${req.query.date}"`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            data,
            state:200
        });
    });
};
// 房屋管理列表数据删除
exports.housingdel = (req, res) => {
    var sql = `delete from housing where date = ${req.query.date}`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }
        res.send({
            data,
            state:200
        });
    });
};



// 查询房屋信息--查看
exports.housinginfomation = (req, res) => {
    var sql = `SELECT * FROM housing WHERE owner_id=${req.body.owner_id} AND date=${req.body.date}`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }else{
            res.send({
                data,
                state:200
            });
        }
        
    });
};